package com.cloud.lan.util;

import com.alibaba.fastjson.JSONArray;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;

import cn.hutool.core.date.DateUtil;

/**
 * 测试main
 * @author ErKang
 * @date Created in 2022/6/29 00:35
 * @title ExcelExportTest.java
 * Department: Product development
 */
public class ExcelExportTest {

    public static void main(String[] args) throws IOException {
        /**
         * 模拟100W条数据,存入JsonArray,此处使用fastJson(号称第一快json解析)快速解析大数据量数据
         * 至于容量问题,Java数组的length必须是非负的int，所以它的理论最大值就是java.lang.Integer.MAX_VALUE = 2^31-1 = 2147483647。
         * 由于xlsx最大支持行数为1048576行,此处模拟了1048573调数据,剩下的3条占用留给自定义的excel的头信息和列项.
         */
        int count = 500000;
//		 int count = 1000000;
//		int count = 1048574;
        JSONArray studentArray = new JSONArray();
        for (int i = 0; i < count; i++) {
            Student s = new Student();
            s.setName("POI-" + i);
            s.setAge(i);
            s.setBirthday(new Date());
            s.setHeight(i);
            s.setWeight(i);
            s.setSex(i % 2 != 0);
            studentArray.add(s);
        }

        /*
         * titleList存放了2个元素,分别为titleMap和headMap
         */
        ArrayList<LinkedHashMap> titleList = new ArrayList<>();
        // 1.titleMap存放了该excel的头信息
        LinkedHashMap<String, String> titleMap = new LinkedHashMap<>();
        titleMap.put("title1", "用户数据全量导出");
        titleMap.put("title2", "导出日期：" + DateUtil.date().toDateStr());
        // 2.headMap存放了该excel的列项
        LinkedHashMap<String, String> headMap = new LinkedHashMap<>();
        headMap.put("name", "姓名");
        headMap.put("age", "年龄");
        headMap.put("birthday", "生日");
        headMap.put("height", "身高");
        headMap.put("weight", "体重");
        headMap.put("sex", "性别");

        titleList.add(titleMap);
        titleList.add(headMap);

        File file = new File("configure/demo/ExcelExportDemo/");
        if (!file.exists()) {
            file.mkdirs();// 创建该文件夹目录
        }
        OutputStream os = null;
        try {
            System.out.println("正在导出xlsx...");
            long start = System.currentTimeMillis();
            // .xlsx格式
            os = new FileOutputStream(file.getAbsolutePath() + File.separator + start + ".xlsx");
//            ExcelUtil.exportExcel(titleList, studentArray, os);
            System.out.println("导出完成...共" + count + "条数据,用时" + (System.currentTimeMillis() - start) + "毫秒");
            System.out.println("文件路径：" + file.getAbsolutePath() + File.separator + start + ".xlsx");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                os.close();
            }
        }

    }
}
